#coding = utf-8

import numpy as np

class PCA:

	def __init__(self, n_components):
		assert n_components >= 1, "n_components must be valid"

		self.n_components = n_components
		self.components_ = None

	def fit(self, X, eta = 0.001, max_iter = 1e4):
		assert self.n_components <= X.shape[1], "n_components must be less than or equal to the columns of X"

		def demean(X):
			return X - np.mean(X, axis=0)

		def f(w, X):
			return np.sum((X.dot(w)) ** 2) / len(w)

		def df(w, X):
			return X.T.dot(X.dot(w)) * 2 / len(w)

		def direction(w):
			return w / np.linalg.norm(w)

		def first_component(X, initail_w, eta, max_iter=1e4, epsilon=1e-8):
			w = direction(initail_w)
			cur_iter = 0

			while cur_iter < max_iter:
				gradient = df(w, X)
				last_w = w
				w = w + eta * gradient
				w = direction(w)

				if abs(f(last_w, X) - f(w, X)) < epsilon:
					break

				cur_iter += 1

			return w

		self.components_ = np.empty(shape=(self.n_components, X.shape[1]))
		X_pca = X.copy()

		for i in range(self.n_components):
			initial_w = np.random.random(X_pca.shape[1])
			w = first_component(X_pca, initial_w, eta)
			self.components_[i,:] = w

			X_pca = X_pca - X_pca.dot(w).reshape(-1, 1) * w


		return self


	def transform(self, X):
		assert X.shape[1] == self.components_.shape[1], "size must be same"

		return X.dot(self.components_.T)

	def inverse_transform(self, X):
		assert X.shape[1] == self.components_.shape[0]

		return X.dot(self.components_)


	def __repr__(self):
		return "PCA(n_components = %d)" % self.n_components
